package com.ant.business.ocr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import com.ant.common.utils.FastMap;
import com.ant.common.utils.LocalBeanUtil;
import com.ant.common.utils.UserThreadLocal;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ant.business.ocr.entity.Medicine;
import com.ant.business.ocr.mapper.MedicineMapper;
import com.ant.common.exceptions.BusinessException;
import com.ant.business.ocr.service.MedicineService;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * 药品表Service实现类
 *
 * @description
 * @create 2023-08-17 16:25:53
 */
@Service("medicineService")
@Transactional(rollbackFor = Throwable.class)
public class MedicineServiceImpl extends ServiceImpl<MedicineMapper, Medicine> implements MedicineService {

    private final MedicineMapper medicineMapper;

    public MedicineServiceImpl(MedicineMapper medicineMapper) {
        this.medicineMapper = medicineMapper;
    }

    /**
     * 分页查询列表
     *
     * @param page 页数
     * @param limit 条数
     * @param param 参数
     * @return PageInfo
     */
    @Override
    public PageInfo<Medicine> getList(int page, int limit, FastMap param) {
        LambdaQueryWrapper<Medicine> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Medicine::getId);
        PageHelper.startPage(page, limit);
        List<Medicine> list = medicineMapper.selectList(wrapper);
        return new PageInfo<Medicine>(list);
    }

    /**
     * 分页查询列表
     *
     * @param param 参数
     * @return PageInfo
     */
    @Override
    public List<Medicine> getAllList(FastMap param) {
        QueryWrapper<Medicine> wrapper = new QueryWrapper<>();
        return medicineMapper.selectList(wrapper);
    }

    /**
     * 通过编号查询
     *
     * @param primaryKey
     * @return
     */
    @Override
    @SneakyThrows
    public Medicine getById(String primaryKey) {
        Medicine medicine = medicineMapper.selectById(primaryKey);
        if (Objects.isNull(medicine)) {
            throw new BusinessException("药品表编号不正确");
        }
        return medicine;
    }

    /**
     * 新增信息
     * @param medicine medicine
     */
    @Override
    @SneakyThrows
    public void saveOne(Medicine medicine) {
      //  LocalBeanUtil.setLoginUserInfo(medicine, UserThreadLocal.getUser().getUserName(), UserThreadLocal.getUser().getUnitId());
        int count = medicineMapper.insert(medicine);
        if (count != 1){
            throw new BusinessException("药品表保存失败");
        }
    }

    /**
     * 更新信息
     * @param medicine medicine
     */
    @Override
    @SneakyThrows
    public void update(Medicine medicine) {
        LocalBeanUtil.updateLoginUserInfo(medicine, UserThreadLocal.getUser().getUserName());
        int count = medicineMapper.updateById(medicine);
        if (count != 1){
            throw new BusinessException("药品表更新失败");
        }
    }

    /**
     * 删除信息
     * @param primaryKey id
     */
    @Override
    @SneakyThrows
    public void delete(String primaryKey) {
        Medicine medicine = medicineMapper.selectById(primaryKey);
        if (Objects.isNull(medicine)) {
            throw new BusinessException("未通过编号找到药品表");
        }
        int count = medicineMapper.deleteById(primaryKey);
        if (count != 1){
            throw new BusinessException("药品表删除失败");
        }
    }
}
